package com.naizhuai.util;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.naizhuai.Model.db.user.PersonUserBean;
import com.naizhuai.R;
import com.naizhuai.ui.entity.management.CompanyBean;
import com.naizhuai.ui.presenter.InputUserNamePresenter;
import com.naizhuai.ui.presenter.InputUserPhonePresenter;
import com.naizhuai.ui.presenter.RequestUserDataListener;
import com.naizhuai.util.autocomplete.Autocomplete;
import com.naizhuai.util.autocomplete.AutocompleteCallback;
import com.naizhuai.util.autocomplete.AutocompletePresenter;

/**
 * 添加人员工具类
 * Created by code_han on 2018/3/5.
 */

public class SearchPersonByDialogUtils {

    private Context mContext;

    private int inputType;

    private PersonUserBean mainManager;

    private AddPersonListener addListener;

    private RequestUserDataListener searchUserDataListener;
    //电话添加的方式
    public static final int INPUT_PHONE_TYPE = 0;

    //名称添加的方式
    public static final int INPUT_NAME_TYPE = 1;

    public SearchPersonByDialogUtils(Context mContext, int inputType, AddPersonListener addListener) {
        this.mContext = mContext;
        this.addListener = addListener;
        this.inputType = inputType;
    }

    public void setSearchUserListener(RequestUserDataListener searchUserDataListener){
        this.searchUserDataListener=searchUserDataListener;
    }

    public void initView() {
        View dialogView = initDialogView();
        AutocompletePresenter<PersonUserBean> presenter;
        String titleStr;
        if(inputType==INPUT_PHONE_TYPE){
            presenter= (InputUserPhonePresenter) dialogView.getTag();
            titleStr=mContext.getText(R.string.input_phone_number).toString();
        }else{
            titleStr=mContext.getText(R.string.input_name).toString();
            presenter= (InputUserNamePresenter) dialogView.getTag();
        }
        DialogUtils.showCustomizeDialog(mContext, titleStr, dialogView, new DialogUtils.DialogListener() {
            @Override
            public boolean onClick(View view) {
                if(mainManager==null){
                    ToastCompat.showToast(mContext, mContext.getText(R.string.chose_true_number).toString(), Toast.LENGTH_SHORT);
                    return false;
                }
                String str = presenter.getSubmitMessage().toString();
                StringBuilder sbInput=new StringBuilder();
                sbInput.append(mainManager.getNickName()).append("@").append(mainManager.getPhoneNumber());
                if (!str.equals(sbInput.toString())) {
                    ToastCompat.showToast(mContext, mContext.getText(R.string.chose_true_number).toString(), Toast.LENGTH_SHORT);
                    return false;
                }
                addListener.onClick(mainManager);
                return true;
            }
        });
    }

    private View initDialogView() {
        View dialogView = LayoutInflater.from(mContext).inflate(R.layout.dialog_add_person_message, null);
        EditText inputEdit = dialogView.findViewById(R.id.input_phone_number);
        LinearLayout powerLayout = dialogView.findViewById(R.id.chose_add_power);
        powerLayout.setVisibility(View.GONE);
        AutocompletePresenter<PersonUserBean> presenter = setEditAutocomplete(inputEdit);
        if(searchUserDataListener!=null)
            presenter.setRequestUserDataListener(searchUserDataListener);
        dialogView.setTag(presenter);
        return dialogView;
    }

    private AutocompletePresenter<PersonUserBean> setEditAutocomplete(EditText edit) {
        float elevation = 6f;
        Drawable backgroundDrawable = new ColorDrawable(Color.WHITE);
        AutocompletePresenter<PersonUserBean> presenter;
        if (inputType == INPUT_PHONE_TYPE)
            presenter = new InputUserPhonePresenter(mContext,SharedPreferenceUtil.getInstance(mContext).getValue(CompanyBean.COMPANY_ID_KEY,""));
        else
            presenter = new InputUserNamePresenter(mContext,SharedPreferenceUtil.getInstance(mContext).getValue(CompanyBean.COMPANY_ID_KEY,""));
        AutocompleteCallback<PersonUserBean> callback = new AutocompleteCallback<PersonUserBean>() {
            @Override
            public boolean onPopupItemClicked(Editable editable, PersonUserBean item) {
                editable.clear();
                mainManager = item;
                editable.append(item.getNickName()).append("@").append(item.getPhoneNumber());
                return true;
            }

            public void onPopupVisibilityChanged(boolean shown) {
            }
        };

        Autocomplete<PersonUserBean> userAutocomplete;
        userAutocomplete = Autocomplete.<PersonUserBean>on(edit)
                .with(elevation)
                .with(backgroundDrawable)
                .with(presenter)
                .with(callback)
                .build();
        return presenter;
    }

    public interface AddPersonListener {
        void onClick(PersonUserBean user);

    }

}
